package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author : [wangtao]
 * @className : OrderServiceImpl
 * @description : []
 * @createTime : [2022/10/14 15:47]
 * @updateTime : [2022/10/14 15:47]
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
  @Autowired
 private   UserService userService;
  @Autowired
  private AddressBookService addressBookService;
  @Autowired
  private ShoppingCartService shoppingCartService;
@Autowired
private OrderDetailService orderDetailService;
    @Override
    public void submit(Orders orders) {

        Long userId = BaseContext.getCurrentId();
        //查询用户表获得 userId,phone,userNmae
        User user = userService.getById(userId);

        orders.setUserName(user.getName());
        orders.setPhone(user.getPhone());
        orders.setUserId(userId);

        //设置订单号 number
       long orderId = IdWorker.getId();

        orders.setNumber(String.valueOf(orderId));
       orders.setId(orderId);
        //设置下单时间 orderTime
        orders.setOrderTime(LocalDateTime.now());
        //设置结账时间 checkoutTime
        orders.setCheckoutTime(LocalDateTime.now());
        //查询 address_book表   address, consignee,
        AddressBook address = addressBookService.getById(orders.getAddressBookId());
        orders.setAddress(address.getDetail());
        orders.setConsignee(address.getConsignee());
        //查询 购物车
        //shoppingCartService 获取 amount
        //保护安全, 原子操作
        AtomicInteger amount = new AtomicInteger(0);
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        List<OrderDetail> collect = shoppingCarts.stream().map((index)->{
            OrderDetail orderDetail = new OrderDetail();
            amount.addAndGet(index.getAmount().multiply(new BigDecimal(index.getNumber())).intValue());
            BeanUtils.copyProperties(index,orderDetail);
            orderDetail.setOrderId(orderId);
            return orderDetail;
        }).collect(Collectors.toList());
       orders.setAmount(new BigDecimal(amount.get()));
this.save(orders);
        orderDetailService.saveBatch(collect);
        shoppingCartService.remove(lqw);
    }
}
